package com.freesky.biz.psbc.controller;

import com.freesky.biz.psbc.domain.TPsbcParity;
import com.freesky.biz.psbc.domain.TPsbcParityItem;
import com.freesky.biz.psbc.service.IPsbcParityService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.xml.transform.Result;
import java.util.List;

@RestController
@RequestMapping("/psbc/parity")
public class PsbcParityController extends BaseController {

    @Autowired
    private IPsbcParityService psbcParityService;

    /**
     * 获取活动配置列表
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:list')")
    @GetMapping("/list")
    public TableDataInfo list(TPsbcParity psbcParity) {
        startPage();
        List<TPsbcParity> list = psbcParityService.findList(psbcParity);
        return getDataTable(list);
    }

    /**
     * 根据ID获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable String configId) {
        TPsbcParity psbcParity = psbcParityService.get(configId);
        List<TPsbcParityItem> psbcParityItemList = psbcParityService.getParityItemByParityId(configId);
        if(psbcParityItemList!=null) {
            psbcParity.setParityItemList(psbcParityItemList);
        }
        return success(psbcParity);
    }

    /**
     * 新增活动配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:add')")
    @Log(title = "邮储比价配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TPsbcParity psbcParity) {
        psbcParity.setId(SeqUtil.getSeqId());
        psbcParity.setName(psbcParity.getName().trim());
        psbcParity.setCreateBy(String.valueOf(getUserId()));
        TPsbcParity parityData = psbcParityService.getByName(psbcParity.getName());
        if(parityData!=null){
            return AjaxResult.error("名称已存在，请核查!");
        }
        return toAjax(psbcParityService.insert(psbcParity));
    }

    /**
     * 修改活动配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:edit')")
    @Log(title = "邮储比价配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TPsbcParity psbcParity) {
        psbcParity.setName(psbcParity.getName().trim());
        psbcParity.setUpdateBy(String.valueOf(getUserId()));
        TPsbcParity parityData = psbcParityService.getByName(psbcParity.getName());
        if((parityData!=null) && (!parityData.getId().equalsIgnoreCase(psbcParity.getId()))){
            return AjaxResult.error("名称已存在，请核查!");
        }
        return toAjax(psbcParityService.update(psbcParity));
    }


    /**
     * 删除活动配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:remove')")
    @Log(title = "活动配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable String[] configIds) {
        psbcParityService.deleteByIds(configIds);
        return success();
    }

    /**
     * 根据ID获取活动规则
     */
    @PreAuthorize("@ss.hasPermi('activity:config:query')")
    @GetMapping("/getParityItemById")
    public AjaxResult getParityItemById(@NotNull String id) {
        TPsbcParityItem psbcParityItem = psbcParityService.getParityItemById(id);
        return success(psbcParityItem);
    }

    /**
     * 新增活动规则配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:add')")
    @Log(title = "活动规则配置", businessType = BusinessType.INSERT)
    @PostMapping("/parityItem")
    public AjaxResult addParityItem(@Validated @RequestBody TPsbcParityItem psbcParityItem) {
        psbcParityItem.setId(SeqUtil.getSeqId());
        psbcParityItem.setItemName(psbcParityItem.getItemName().trim());
        return toAjax(psbcParityService.addParityItem(psbcParityItem));
    }

    /**
     * 新增活动规则配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:add')")
    @Log(title = "活动规则配置", businessType = BusinessType.UPDATE)
    @PutMapping("/parityItem")
    public AjaxResult modifyParityItem(@Validated @RequestBody TPsbcParityItem psbcParityItem) {
        psbcParityItem.setItemName(psbcParityItem.getItemName().trim());
        return toAjax(psbcParityService.updateParityItem(psbcParityItem));
    }

    /**
     * 删除活动规则配置
     */
    @PreAuthorize("@ss.hasPermi('psbc:parity:remove')")
    @Log(title = "活动规则配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/parityItem/{configId}")
    public AjaxResult remove(@PathVariable String configId) {
        psbcParityService.deleteParityItemById(configId);
        return success();
    }
}
